//
//  BJCompareImageView.m
//  SZBank
//
//  Created by rimi on 16/7/18.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJCompareImageView.h"

@interface BJCompareImageView ()
{
    CGFloat imageWidth;/**< 图片宽度 */
    CGFloat imageHeight;/**< 图片高度 */
    NSMutableArray *OrangeDataArray;/**< 数据源数组 */
}
@property (nonatomic)UIButton *resetButton;/**< 重置button */
@property (nonatomic)UIButton *closeButton;/**< 关闭button */
@property (nonatomic)UIButton *checkButton;/**< 查看图标 */

@end

@implementation BJCompareImageView


#pragma mark -- 初始化
- (instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray *)dataArray
{
    self = [super initWithFrame:frame];
    if (self) {
        imageWidth = KWIDTH * 0.6;
        imageHeight = KHEIGHT * 0.5;
     
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.675];
  
  
     
    

        OrangeDataArray = [NSMutableArray array];
        OrangeDataArray = [dataArray mutableCopy];
        
      

        
        [self creatTableToImageViewWithArray:OrangeDataArray];
       
    }
    return self;
}

#pragma mark -- actions


/**
 *      添加图标方法
 */
- (void)creatTableToImageViewWithArray:(NSArray *)dataArray {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH * 0.2, KHEIGHT * 0.2, KWIDTH * 0.6, KHEIGHT * 0.6)];
    imageView.image = [LocalDataManager  getImageWithSourceOfPath:@"理财_对比框"];
    imageView.userInteractionEnabled = YES;
    
    [imageView addSubview:self.checkButton];
    [imageView addSubview:self.closeButton];
    [imageView addSubview:self.resetButton];
    
    
    
    //循环创建第一行
    //创建第一行字符串数组
    NSArray *array_firstRow = @[@"产品名称",@"期限（天）",@"收益率（%）",@"起点金额（万）"];
    //
    for (int i = 0; i < 4; i ++) {
        UIView *firstLine = [[UIView alloc]init];
        //线条背景颜色
        firstLine.backgroundColor =[UIColor colorWithRed:117 / 255.0 green:97/ 255.0 blue:26 / 255.0 alpha:1.0];
        
        
        UILabel *label = [[UILabel alloc]init];

        label.text = array_firstRow[i];
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        label.textAlignment = NSTextAlignmentCenter;

        
        if (i == 3) {
            firstLine.frame = CGRectMake(imageWidth * 0.01, imageHeight * 0.14, imageWidth * 0.98, imageHeight * 0.003);
             label.frame = CGRectMake( imageWidth * 0.325 + imageWidth * 0.22 * (i - 1) , imageHeight * 0.022, imageWidth * 0.2, imageHeight * 0.12);
        }else{
            if (i == 0) {
                label.frame = CGRectMake( imageWidth * 0.022 , imageHeight * 0.022, imageWidth * 0.28, imageHeight * 0.12);
            }else{
                label.frame = CGRectMake( imageWidth * 0.325 + imageWidth * 0.22 * (i - 1) , imageHeight * 0.022, imageWidth * 0.2, imageHeight * 0.12);
            }
        
            firstLine.frame =CGRectMake(imageWidth * 0.3 + i * imageWidth * 0.225 , 0, imageHeight * 0.003, imageHeight * 0.14);
            
        }
        //加入视图
        [imageView addSubview:label];
        [imageView addSubview:firstLine];
    }
    //循环创建需要加入数据的行
    

    
    if (dataArray.count != 0) {
//列
        for (int i = 0; i < 3; i ++) {
            UIView  *rankLine = [[UIView alloc]init];/**< 列线条 */
            rankLine.frame = CGRectMake(imageWidth * 0.3 + i * imageWidth * 0.225 , imageHeight * 0.14 , imageHeight * 0.003, imageHeight * 0.14 * (dataArray.count));
             rankLine.backgroundColor = [UIColor colorWithRed:117 / 255.0 green:97/ 255.0 blue:26 / 255.0 alpha:1.0];
            [imageView addSubview:rankLine];
        }
//行
        for ( int i = 0; i < dataArray.count; i ++) {
            UIView *rowLine = [[UIView alloc]initWithFrame:CGRectMake(imageWidth * 0.01, imageHeight * 0.14 * ((i + 1) + 1), imageWidth * 0.98, imageHeight * 0.003)];/**< 行线条 */
            rowLine.backgroundColor = [UIColor colorWithRed:117 / 255.0 green:97/ 255.0 blue:26 / 255.0 alpha:1.0];
            [imageView addSubview:rowLine];
            
//加入label
            for (int j = 0; j < 4; j ++) {
                UILabel *label =[[UILabel alloc]init];
                label.font = [UIFont systemFontOfSize:FONT_SIZE];
                label.textAlignment = NSTextAlignmentCenter;
                label.text = dataArray[i][@"dataSource"][j];
                label.adjustsFontSizeToFitWidth = YES;
           
                
                if (j == 0) {
                    label.frame = CGRectMake( imageWidth * 0.022 , imageHeight * 0.022 + imageHeight * 0.13 * (i + 1), imageWidth * 0.28, imageHeight * 0.12);
                }else{
                    label.frame = CGRectMake( imageWidth * 0.325 + imageWidth * 0.22 * (j - 1) , imageHeight * 0.022 + imageHeight * 0.13 * (i + 1), imageWidth * 0.2, imageHeight * 0.12);
                }
                
                [imageView addSubview:label];

            }
  
        }
        
    }

    
     [self addSubview:imageView];
}


/**
 * button点击事件
 */
- (void)action_buttonClick:(UIButton *)sender{
    if (sender.tag == 1000) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        OrangeDataArray = nil;
        [self creatTableToImageViewWithArray:OrangeDataArray];
        
        
        //通知传值
        NSNotification *notification = [NSNotification notificationWithName:@"BJnotification" object:nil];
       [[NSNotificationCenter defaultCenter] postNotification:notification];

    }else if (sender.tag == 1001){
        
    }else{
               OrangeDataArray = nil;
        [self creatTableToImageViewWithArray:OrangeDataArray];
        [self removeFromSuperview];
    }
}
#pragma mark -- getters
- (UIButton *)resetButton {
    if (!_resetButton) {
        _resetButton = ({
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(imageWidth * 0.25, imageHeight, imageWidth * 0.2, imageHeight * 0.1)];
            [button setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_重置"] forState:UIControlStateNormal];
            button.tag = 1000;
            [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button;
        
        });
    }
    return _resetButton;
}

- (UIButton *)checkButton{
    if (!_checkButton) {
        _checkButton = ({
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(imageWidth * 0.5, imageHeight, imageWidth * 0.2, imageHeight * 0.1)];
        
            [button setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_查看图标"] forState:UIControlStateNormal];
             button.tag = 1001;
                [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _checkButton;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = ({
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(imageWidth * 0.75, imageHeight, imageWidth * 0.2, imageHeight * 0.1)];
         
             [button setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_关闭"] forState:UIControlStateNormal];
             button.tag = 1002;
                [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button;});
    }
    return _closeButton;
}
@end
